using System;
using System.Configuration;
using System.Data;
using System.Collections;
using digit.Core;


namespace SMS.ServiceManager
{
    public class MessageOutManager
    {
        #region CONSTRUCTORS
        static MessageOutManager()
        {
            _data = new MessageOutData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static MessageOutData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(long msgInID, string mobileNo, string receiverMobileNo, string serviceID, string commandCode, string subCode1, string subCode2, string subCode3, string msgOutContent, string msgInContent, long requestID, int msgType, int contentType, DateTime requestTime, DateTime responseTime, string messageID, int isCDR, int isMore, int msgTotal, int msgIndex, int status)
        {
            _error = "";
            try
            {
                object[] values = new object[21];
                values[0] = msgInID;
                values[1] = Util.ReplaceNullTo(mobileNo, DBNull.Value);
                values[2] = Util.ReplaceNullTo(receiverMobileNo, DBNull.Value);
                values[3] = Util.ReplaceNullTo(serviceID, DBNull.Value);
                values[4] = Util.ReplaceNullTo(commandCode, DBNull.Value);
                values[5] = Util.ReplaceNullTo(subCode1, DBNull.Value);
                values[6] = Util.ReplaceNullTo(subCode2, DBNull.Value);
                values[7] = Util.ReplaceNullTo(subCode3, DBNull.Value);
                values[8] = Util.ReplaceNullTo(msgOutContent, DBNull.Value);
                values[9] = Util.ReplaceNullTo(msgInContent, DBNull.Value);
                values[10] = Util.ReplaceNullTo(requestID, DBNull.Value);
                values[11] = Util.ReplaceNullTo(msgType, DBNull.Value);
                values[12] = Util.ReplaceNullTo(contentType, DBNull.Value);
                values[13] = Util.ReplaceNullTo(requestTime, DBNull.Value);
                values[14] = Util.ReplaceNullTo(responseTime, DBNull.Value);
                values[15] = Util.ReplaceNullTo(messageID, DBNull.Value);
                values[16] = Util.ReplaceNullTo(isCDR, DBNull.Value);
                values[17] = Util.ReplaceNullTo(isMore, DBNull.Value);
                values[18] = Util.ReplaceNullTo(msgTotal, DBNull.Value);
                values[19] = Util.ReplaceNullTo(msgIndex, DBNull.Value);
                values[20] = status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(MessageOutInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[21];
                values[0] = info.MsgInID;
                values[1] = Util.ReplaceNullTo(info.MobileNo, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.ReceiverMobileNo, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.ServiceID, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.CommandCode, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.SubCode1, DBNull.Value);
                values[6] = Util.ReplaceNullTo(info.SubCode2, DBNull.Value);
                values[7] = Util.ReplaceNullTo(info.SubCode3, DBNull.Value);
                values[8] = Util.ReplaceNullTo(info.MsgOutContent, DBNull.Value);
                values[9] = Util.ReplaceNullTo(info.MsgInContent, DBNull.Value);
                values[10] = Util.ReplaceNullTo(info.RequestID, DBNull.Value);
                values[11] = Util.ReplaceNullTo(info.MsgType, DBNull.Value);
                values[12] = Util.ReplaceNullTo(info.ContentType, DBNull.Value);
                values[13] = Util.ReplaceNullTo(info.RequestTime, DBNull.Value);
                values[14] = Util.ReplaceNullTo(info.ResponseTime, DBNull.Value);
                values[15] = Util.ReplaceNullTo(info.MessageID, DBNull.Value);
                values[16] = Util.ReplaceNullTo(info.IsCDR, DBNull.Value);
                values[17] = Util.ReplaceNullTo(info.IsMore, DBNull.Value);
                values[18] = Util.ReplaceNullTo(info.MsgTotal, DBNull.Value);
                values[19] = Util.ReplaceNullTo(info.MsgIndex, DBNull.Value);
                values[20] = info.Status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(long msgOutID, long msgInID, string mobileNo, string receiverMobileNo, string serviceID, string commandCode, string subCode1, string subCode2, string subCode3, string msgOutContent, string msgInContent, long requestID, int msgType, int contentType, DateTime requestTime, DateTime responseTime, string messageID, int isCDR, int isMore, int msgTotal, int msgIndex, int status)
        {
            _error = "";
            try
            {
                object[] values = new object[22];
                values[0] = msgOutID;
                values[1] = msgInID;
                values[2] = Util.ReplaceNullTo(mobileNo, DBNull.Value);
                values[3] = Util.ReplaceNullTo(receiverMobileNo, DBNull.Value);
                values[4] = Util.ReplaceNullTo(serviceID, DBNull.Value);
                values[5] = Util.ReplaceNullTo(commandCode, DBNull.Value);
                values[6] = Util.ReplaceNullTo(subCode1, DBNull.Value);
                values[7] = Util.ReplaceNullTo(subCode2, DBNull.Value);
                values[8] = Util.ReplaceNullTo(subCode3, DBNull.Value);
                values[9] = Util.ReplaceNullTo(msgOutContent, DBNull.Value);
                values[10] = Util.ReplaceNullTo(msgInContent, DBNull.Value);
                values[11] = Util.ReplaceNullTo(requestID, DBNull.Value);
                values[12] = Util.ReplaceNullTo(msgType, DBNull.Value);
                values[13] = Util.ReplaceNullTo(contentType, DBNull.Value);
                values[14] = Util.ReplaceNullTo(requestTime, DBNull.Value);
                values[15] = Util.ReplaceNullTo(responseTime, DBNull.Value);
                values[16] = Util.ReplaceNullTo(messageID, DBNull.Value);
                values[17] = Util.ReplaceNullTo(isCDR, DBNull.Value);
                values[18] = Util.ReplaceNullTo(isMore, DBNull.Value);
                values[19] = Util.ReplaceNullTo(msgTotal, DBNull.Value);
                values[20] = Util.ReplaceNullTo(msgIndex, DBNull.Value);
                values[21] = status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(MessageOutInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[22];
                values[0] = info.MsgOutID;
                values[1] = info.MsgInID;
                values[2] = Util.ReplaceNullTo(info.MobileNo, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.ReceiverMobileNo, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.ServiceID, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.CommandCode, DBNull.Value);
                values[6] = Util.ReplaceNullTo(info.SubCode1, DBNull.Value);
                values[7] = Util.ReplaceNullTo(info.SubCode2, DBNull.Value);
                values[8] = Util.ReplaceNullTo(info.SubCode3, DBNull.Value);
                values[9] = Util.ReplaceNullTo(info.MsgOutContent, DBNull.Value);
                values[10] = Util.ReplaceNullTo(info.MsgInContent, DBNull.Value);
                values[11] = Util.ReplaceNullTo(info.RequestID, DBNull.Value);
                values[12] = Util.ReplaceNullTo(info.MsgType, DBNull.Value);
                values[13] = Util.ReplaceNullTo(info.ContentType, DBNull.Value);
                values[14] = Util.ReplaceNullTo(info.RequestTime, DBNull.Value);
                values[15] = Util.ReplaceNullTo(info.ResponseTime, DBNull.Value);
                values[16] = Util.ReplaceNullTo(info.MessageID, DBNull.Value);
                values[17] = Util.ReplaceNullTo(info.IsCDR, DBNull.Value);
                values[18] = Util.ReplaceNullTo(info.IsMore, DBNull.Value);
                values[19] = Util.ReplaceNullTo(info.MsgTotal, DBNull.Value);
                values[20] = Util.ReplaceNullTo(info.MsgIndex, DBNull.Value);
                values[21] = info.Status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(long msgOutID)
        {
            _error = "";
            try
            {
                object[] values = { msgOutID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(MessageOutInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.MsgOutID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static MessageOutInfo Select(long msgOutID)
        {
            _error = "";
            try
            {
                object[] values = { msgOutID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                MessageOutInfo info = new MessageOutInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool Update_MsgInOut_Loading()
        {
            _error = "";
            try
            {
                return _data.Update_MsgInOut_Loading();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static DataTable GetMsgOut()
        {
            _error = "";
            try
            {
                if (Util.GetAppSettings("Version").Trim().ToLower() == "vms")
                    return _data.GetMsgOutVms();
                else if (Util.GetAppSettings("Version").Trim().ToLower() == "gpc")
                    return _data.GetMsgOutGpc();
                else if (Util.GetAppSettings("Version").Trim().ToLower() == "viettel" || Util.GetAppSettings("Version").Trim().ToLower() == "vtel")
                    return _data.GetMsgOutViettel();

                return _data.GetMsgOut();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static ArrayList GetAllMessages_ForSend()
        {
            DataSet set = _data.GetAllMessages_ForSend();
            if (set.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            ArrayList outs = new ArrayList();
            foreach (DataRow row in set.Tables[0].Rows)
            {
                MessageOutInfo @out = new MessageOutInfo();
                @out.MsgOutID = long.Parse(row["MsgOutID"].ToString());
                @out.MsgInID = long.Parse(row["MsgInID"].ToString());
                @out.MobileNo = row["MobileNo"].ToString();
                @out.ReceiverMobileNo = row["ReceiverMobileNo"].ToString();
                @out.ServiceID = row["ServiceID"].ToString();
                @out.CommandCode = row["CommandCode"].ToString();
                @out.SubCode1 = row["SubCode1"].ToString();
                @out.SubCode2 = row["SubCode2"].ToString();
                @out.MsgOutContent = row["MsgOutContent"].ToString();
                @out.MsgInContent = row["MsgInContent"].ToString();
                @out.RequestID = long.Parse(row["RequestID"].ToString());
                @out.MsgType = int.Parse(row["MsgType"].ToString());
                @out.ContentType = int.Parse(row["ContentType"].ToString());
                @out.RequestTime = Convert.ToDateTime(row["RequestTime"].ToString());
                @out.ResponseTime = Convert.ToDateTime(row["ResponseTime"].ToString());
                @out.IsMore = int.Parse(row["IsMore"].ToString());
                @out.MsgTotal = int.Parse(row["MsgTotal"].ToString());
                @out.Status = int.Parse(row["Status"].ToString());
                @out.MsgIndex = int.Parse(row["MsgIndex"].ToString());
                outs.Add(@out);
            }
            return outs;
        }

        public static DataTable SelectAll_Network_ByServiceID(string serviceId)
        {
            _error = "";
            try
            {
                return _data.SelectAll_Network_ByServiceID(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgOut_ByServiceID_Online(string serviceId)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgOut_ByServiceID_Online(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgCdr_ByServiceID_Online(string serviceId)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgCdr_ByServiceID_Online(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SearchMsgOut_ByServiceID_Online(string serviceId, string content, DateTime fromDate, DateTime toDate, int typ)
        {
            _error = "";
            try
            {
                object[] values = { serviceId, content, fromDate, toDate, typ };
                return _data.SearchMsgOut_ByServiceID_Online(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgOut_ByService_Online(string service)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgOut_ByService_Online(service);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgCdr_ByService_Online(string service)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgCdr_ByService_Online(service);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgOut_ByServiceID(string serviceId)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgOut_ByServiceID(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgCdr_ByServiceID(string serviceId)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgCdr_ByServiceID(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgOut_ByService(string service)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgOut_ByService(service);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgCdr_ByService(string service)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgCdr_ByService(service);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool UpdateStatus(long MsgOutID, long RequestID, DateTime ResponseTime, int Index, int Status)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = MsgOutID;
                values[1] = RequestID;
                values[2] = ResponseTime;
                values[3] = Index;
                values[4] = Status;

                return _data.UpdateStatus(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static int InsertMT(string MobileNo, string Message, string ServiceID, string CommandCode, string MessageType, string RequestID, string TotalMsg, string MsgIndex, string IsMore, string ContentType)
        {
            _error = "";
            try
            {
                object[] values = new object[10];
                values[0] = MobileNo;
                values[1] = Message;
                values[2] = ServiceID;
                values[3] = CommandCode;
                values[4] = MessageType;
                values[5] = RequestID;
                values[6] = TotalMsg;
                values[7] = MsgIndex;
                values[8] = IsMore;
                values[9] = ContentType;

                return _data.InsertMT(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return 0;
            }
        }

        public static bool InsertError(string RequestID, DateTime ResponseTime, string Error, string Type)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = RequestID;
                values[1] = ResponseTime;
                values[2] = Error;
                values[3] = Type;

                return _data.InsertError(Error);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        #endregion
    }
}